package com.atdel.demo19;


import org.junit.Test;

import java.lang.reflect.Method;

/**
 * @BelongsProject: java_study
 * @BelongsPackage: com.atdel.demo19
 * @Author: Del
 * @CreateTime: 2025-03-17  15:50
 * @Description:
 * 遍历String类中所有的方法，格式如下所示
 *     方法的个数：76
 *     public boolean equals(Object arg0)
 *     public String toString()
 *     public int hashCode()
 *     ...
 *     public int indexOf(String arg0,int arg1)
 *     ...
 *     public static String valueOf(char[] arg0,int arg1,int arg2)
 *     ...
 * @Version: 1.0
 */
public class ReflectDemo {
    @Test
    public void test() {
        // 获取系统类加载器
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
        try {
            // 加载类
            Class<?> aClass = systemClassLoader.loadClass("java.lang.String");

            // 获取所有的公共方法以及私有方法
            Method[] methods = aClass.getDeclaredMethods();
            // 获取所有的方法
            System.out.println("方法的个数：" + methods.length);
            for (Method method : aClass.getMethods()) {
                System.out.println(method);
            }
            System.out.println(methods.length);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
